package com.webdrop.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.webdrop.gui.LoginWidget;
import com.webdrop.gui.Page;
import com.webdrop.utils.HistoryParams;

public class Login extends Page implements ValueChangeHandler<String>{
	
	DialogBox login;
	VerticalPanel popUp;
	Storage sessionStorage;
	private LoginWidget loginWidget;
	
	public Login(){
		
		super();
		History.addValueChangeHandler(this);
		loginWidget = new LoginWidget();
		container.add(loginWidget);
			
	}
		  	
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		HistoryParams p = HistoryParams.getInstance();
		
		if(p.containsKey("status")){
			if (p.get("status").equals("accepted")&& p.containsKey("sessionid") && p.containsKey("userid"))
			{
				sessionStorage = Storage.getSessionStorageIfSupported();
				
				if(sessionStorage!=null){
					sessionStorage.clear();
					String userId = p.get("userid");
					String sessionId = p.get("sessionid");
					String googleId = p.get("googleid");
					
					sessionStorage.setItem("userid", userId);
					sessionStorage.setItem("sessionId", sessionId);
					sessionStorage.setItem("googleId", googleId);
				}
				History.newItem("page=myFilePage");
			
			}
		}
	}
}
